#include "tcp_server.h"

tcp_server::tcp_server(asio::io_service& iosev, unsigned int port) :net_tcp::server(iosev, port)
{

}

void tcp_server::on_connected(net_tcp::shared_session pSession)
{
	if (m_on_session_connect != nullptr)
	{
		m_on_session_connect(pSession->ID());
	}
}

void tcp_server::on_closeed(net_tcp::shared_session pSession)
{
	if (m_on_session_disconnect != nullptr)
	{
		m_on_session_disconnect(pSession->ID());
	}
}

void tcp_server::on_recv(net_tcp::shared_session pSession)
{
	if (m_on_session_message != nullptr)
	{
		net_tcp::buffer& recv_buffer = pSession->get_recv_buffer();
		int readlen = m_on_session_message(pSession->ID(), recv_buffer.read_pointer(), recv_buffer.readable_size());
		if (readlen > 0)
		{
			recv_buffer.increase_read(readlen);
		}
	}
}
void tcp_server::on_error(net_tcp::shared_session pSession)
{
	if (m_on_session_error != nullptr)
	{
		asio::error_code error = pSession->get_last_error();
		m_on_session_error(pSession->ID(),error.value());
	}
}
